package org.madgecko.basic.dao.impl;

import org.madgecko.basic.dao.BaseDao;
import org.madgecko.basic.model.Pager;
import org.madgecko.basic.dao.iface.IUserDao;
import org.madgecko.basic.model.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDao extends BaseDao<User> implements IUserDao  {

	@Override
	public User loadByName(String name) {
		String hql = "from User u where u.username=?";
		return (User)this.queryObject(hql, name);
	}

	@Override
	public Pager<User> findUserByCon(Integer status, String con) {
		String hql = "from User u where u.id>" + 0;
		if(status!=null){
			hql += " and u.status=" + status;
		}
		if(con!=null){
			hql += " and (u.username like '%" + con + "%' or u.realName like '%" + con + "%')";
		}
		return this.find(hql);
	}

	@Override
	public List<User> listUserByRole(Integer roleId) {
		//1条sql
		String hql = "select ur.user from UserRole ur where ur.role.id=?";
		return this.list(hql, roleId);
	}
}
